package com.example.a1725185505.works.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.a1725185505.works.R;
import com.example.a1725185505.works.base.BaseActivity;
import com.example.a1725185505.works.base.BaseListener;
import com.example.a1725185505.works.bean.UserByIdBean;
import com.example.a1725185505.works.bean.updateUserByIdBean;
import com.example.a1725185505.works.model.UserByIdModel;
import com.example.a1725185505.works.model.updateUserByIdModel;
import com.example.a1725185505.works.util.SharedPreferencesUtils;

public class UpdateuserActivity extends BaseActivity {

    private  TextView tv_fanhui;
    private EditText et_update_username , et_update_userpass , et_update_mobile , et_update_address;
    private Button bt_update;
    private Integer user_id;
    private SharedPreferencesUtils sharedPreferencesUtils;
    private UserByIdBean beans;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_updateuser;
    }

    @Override
    public void initView() {

        tv_fanhui = (TextView)findViewById(R.id.tv_fanhui);
        et_update_username = (EditText) findViewById(R.id.et_update_username);
        et_update_userpass = (EditText)findViewById(R.id.et_update_userpass);
        et_update_mobile = (EditText)findViewById(R.id.et_update_mobile);
        et_update_address = (EditText)findViewById(R.id.et_update_address);
        bt_update = (Button)findViewById(R.id.bt_update);
        bt_update.setOnClickListener(this);
        tv_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent();
                i.setClass(UpdateuserActivity.this,HomeActivity.class);
//                i.putExtra("id",3);
                startActivity(i);
            }
        });
    }

    @Override
    public void initEvent() {
        sharedPreferencesUtils = new SharedPreferencesUtils(getContext());
        user_id = Integer.parseInt(sharedPreferencesUtils.readString("userid"));
        showLog(user_id+"");
        UserByIdModel userModel = new UserByIdModel();
        userModel.getuserbyid(user_id , baseListener);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_update:
                updateUserByIdModel updateModel = new updateUserByIdModel();
                updateModel.getupdateuserbyid(user_id , et_update_username.getText().toString() , et_update_userpass.getText().toString(),
                        et_update_mobile.getText().toString() , et_update_address.getText().toString() , listener);
                break;
        }

    }

    BaseListener<UserByIdBean> baseListener = new BaseListener<UserByIdBean>() {
        @Override
        public void onResponse(UserByIdBean userbyidBean) {

            if (null != userbyidBean){
                beans = userbyidBean;
                et_update_username.setText(beans.getUsername());
                et_update_userpass.setText(beans.getUserpass());
                et_update_address.setText(beans.getAddress());
                et_update_mobile.setText(beans.getMobilenum());
            }
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };

    BaseListener<updateUserByIdBean> listener = new BaseListener<updateUserByIdBean>() {
        @Override
        public void onResponse(updateUserByIdBean updateuserBean) {
            if (null != updateuserBean){
                if (updateuserBean.getSuccess().equals("1")){
                    showToast("修改成功");
                    Intent intent = new Intent(UpdateuserActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else {
                    showToast("修改失败");
                }
            }
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };
}
